package com.ymttest.database.sqlwapper.hunter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.hunter.HunterSettlement;

public class HunterSettlementWrapper {

	SqlManager manager;

	public HunterSettlementWrapper() {
		manager = new SqlManager("tradingsettlement");
	}

	@SuppressWarnings("unchecked")
	public List<HunterSettlement> selectByOrderId(Long orderId) {

		List<HunterSettlement> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.selectByOrderId",
						orderId);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("hunter_settlement没有数据");
		}

		return result;
	}

	@SuppressWarnings("unchecked")
	public List<HunterSettlement> selectByHunterId(Long hunterId) {

		List<HunterSettlement> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.selectByHunterId",
						hunterId);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("hunter_settlement没有数据");
		}

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<HunterSettlement> selectByHunterIdAndDate(Long hunterId, int year, int month) {

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("hunterId", hunterId);
		m.put("year", year);
		m.put("month", month);
		
		List<HunterSettlement> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.selectByHunterIdAndDate",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("hunter_settlement没有数据");
		}

		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int updateExpectSettlementTimeByOrderId(Date date, Long orderId) {

		Map m = new HashMap();
		m.put("expect_settlement_time", date);
		m.put("order_id", orderId);

		int result = manager
				.update("com.ymttest.database.mapping.hunter.HunterSettlementMapper.updateExpectSettlementTimeByOrderId",
						m);
		manager.commit();

		if (result == 0) {
			Logger.comment("hunter_settlement没有数据");
		}

		return result;
	}

	public BigDecimal sumUnSettledAmount(Long hunterId) {

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.sumUnSettledAmount",
						hunterId).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	public BigDecimal sumCurrentMonthIncome(Long hunterId) {

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.sumCurrentMonthIncome",
						hunterId).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	public BigDecimal sumTodayIncome(Long hunterId) {

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.sumTodayIncome",
						hunterId).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	public BigDecimal sumTodaySalesAmount(Long hunterId) {

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.sumTodaySalesAmount",
						hunterId).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<Integer, BigDecimal> totalIncomeMonthStatistics(Long hunterId,
			int year) {

		Map map = new HashMap();
		map.put("hunterId", hunterId);
		map.put("year", year);

		List<Map> result = new ArrayList<Map>();

		result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.totalIncomeMonthStatistics",
						map);
		manager.commit();

		Map<Integer, BigDecimal> resultMap = this.listTurnToMap(result);

		return resultMap;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<Integer, BigDecimal> settledIncomeMonthStatistics(Long hunterId,
			int year) {

		Map map = new HashMap();
		map.put("hunterId", hunterId);
		map.put("year", year);

		List<Map> result = new ArrayList<Map>();

		result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.settledIncomeMonthStatistics",
						map);
		manager.commit();

		Map<Integer, BigDecimal> resultMap = this.listTurnToMap(result);

		return resultMap;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public BigDecimal totalAmountYear(Long hunterId, int year) {

		Map map = new HashMap();
		map.put("hunterId", hunterId);
		map.put("year", year);

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.totalAmountYear",
						map).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<Integer, BigDecimal> totalManageFeeIncomeMonthStatistics(
			Long hunterId, int year) {

		Map map = new HashMap();
		map.put("hunterId", hunterId);
		map.put("year", year);

		List<Map> result = new ArrayList<Map>();

		result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.totalManageFeeIncomeMonthStatistics",
						map);
		manager.commit();

		Map<Integer, BigDecimal> resultMap = this.listTurnToMap(result);

		return resultMap;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<Integer, BigDecimal> settledManageFeeIncomeMonthStatistics(
			Long hunterId, int year) {

		Map map = new HashMap();
		map.put("hunterId", hunterId);
		map.put("year", year);

		List<Map> result = new ArrayList<Map>();

		result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.settledManageFeeIncomeMonthStatistics",
						map);
		manager.commit();

		Map<Integer, BigDecimal> resultMap = this.listTurnToMap(result);

		return resultMap;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public BigDecimal totalManageFeeAmountYear(Long hunterId, int year) {

		Map map = new HashMap();
		map.put("hunterId", hunterId);
		map.put("year", year);

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.totalManageFeeAmountYear",
						map).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	public BigDecimal sumCommissionUnSettledAmonut(Long hunterId) {

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.sumCommissionUnSettledAmonut",
						hunterId).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	public BigDecimal sumManageFeeUnSettledAmonut(Long hunterId) {

		BigDecimal result = (BigDecimal) manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.sumManageFeeUnSettledAmonut",
						hunterId).get(0);
		manager.commit();

		if (null == result)
			return BigDecimal.ZERO;

		return result;
	}

	@SuppressWarnings("unchecked")
	public HunterSettlement selectHunterSettlementByOrderIdAndHunterId(
			Long hunterId, Long orderId) {

		List<HunterSettlement> result = new ArrayList<HunterSettlement>();

		Map<String, Long> map = new HashMap<String, Long>();
		map.put("hunterId", hunterId);
		map.put("orderId", orderId);

		result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.hunter.HunterSettlementMapper.selectHunterSettlementByOrderIdAndHunterId",
						map);
		manager.commit();

		if (result.size() == 0)
			return null;

		return result.get(0);
	}

	@SuppressWarnings("rawtypes")
	private Map<Integer, BigDecimal> listTurnToMap(List<Map> list) {

		Map<Integer, BigDecimal> resultMap = new HashMap<Integer, BigDecimal>();

		for (int i = 0; i < list.size(); i++)
			resultMap.put(Integer.valueOf(list.get(i).get("month").toString()),
					new BigDecimal(list.get(i).get("totalIncome").toString()));

		if (resultMap.size() == 12)
			return resultMap;

		for (int k = 0; k < 12; k++) {

			if (null == resultMap.get(k + 1))
				resultMap.put(k + 1, BigDecimal.ZERO);
		}

		return resultMap;
	}
	
	public int insert(HunterSettlement hunterSettlement) {
		
    	int count = manager
				.insert("com.ymttest.database.mapping.hunter.HunterSettlementMapper.insert",
						hunterSettlement);
    	manager.commit();
		return count;
    }

	public static void main(String[] args) {

		HunterSettlementWrapper test = new HunterSettlementWrapper();
		System.out.println(test.selectByHunterIdAndDate(
				20359857L, 2019, 5).get(0).getTotal_amount());
		
//		for (int i = 0; i < 10; i ++) {
//			
//			HunterSettlement hunterSettlement = new HunterSettlement();
//			
//			hunterSettlement.setId(TestDataManager.randomStr(10));
//			hunterSettlement.setBiz_no(TestDataManager.getPhoneNum());
//			hunterSettlement.setSettlement_type(22);
//			hunterSettlement
//					.setOrder_id(Long.valueOf(TestDataManager.getPhoneNum()));
//			hunterSettlement.setHunter_id(24713026L);
//			hunterSettlement.setHunter_level(2);
//			hunterSettlement.setTotal_amount(TestDataManager.randomPrice(
//					new BigDecimal("100"), new BigDecimal("200")));
//			hunterSettlement.setSettlement_status(1);
//			hunterSettlement.setPaid_time(DateTime.now().plusMonths(11).toDate());
//			hunterSettlement.setTotal_product_price(TestDataManager.randomPrice(
//					new BigDecimal("1000"), new BigDecimal("2000")));
//			hunterSettlement.setAdd_time(DateTime.now().toDate());
//			hunterSettlement.setUpdate_time(DateTime.now().toDate());
//
//			test.insert(hunterSettlement);
//			
//		}
	}
}
